package com.ruoyi.web.controller.system;

import java.util.List;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MsgInspectionDto;
import com.ruoyi.system.service.IMsgInspectionDtoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 巡查台账Controller
 *
 * @author ruoyi
 * @date 2021-02-18
 */
@Controller
@RequestMapping("/system/dto")
public class MsgInspectionDtoController extends BaseController
{
    private String prefix = "system/dto";

    @Autowired
    private IMsgInspectionDtoService msgInspectionDtoService;

    @RequiresPermissions("system:dto:view")
    @GetMapping()
    public String dto()
    {
        return prefix + "/dto";
    }

    /**
     * 查询巡查台账列表
     */
    @RequiresPermissions("system:dto:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MsgInspectionDto msgInspectionDto)
    {
        startPage();
        List<MsgInspectionDto> list = msgInspectionDtoService.selectMsgInspectionDtoList(msgInspectionDto);
        return getDataTable(list);
    }

    /**
     * 导出巡查台账列表
     */
    @RequiresPermissions("system:dto:export")
    @Log(title = "巡查台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MsgInspectionDto msgInspectionDto)
    {
        List<MsgInspectionDto> list = msgInspectionDtoService.selectMsgInspectionDtoList(msgInspectionDto);
        ExcelUtil<MsgInspectionDto> util = new ExcelUtil<MsgInspectionDto>(MsgInspectionDto.class);
        return util.exportExcel(list, "dto");
    }

    /**
     * 新增巡查台账
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存巡查台账
     */
    @RequiresPermissions("system:dto:add")
    @Log(title = "巡查台账", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HttpServletRequest httpRequest)
    {
        String string = httpRequest.getParameter("key");
        MsgInspectionDto msgDTO= JSON.parseObject(string, MsgInspectionDto.class);
        return toAjax(msgInspectionDtoService.insertMsgInspectionDto(msgDTO));
    }


    /**
     * 修改巡查台账
     */
    @GetMapping("/edit/{pkid}")
    public String edit(@PathVariable("pkid") Integer pkid, ModelMap mmap)
    {
        MsgInspectionDto msgInspectionDto = msgInspectionDtoService.selectMsgInspectionDtoById(pkid);
        mmap.put("msgInspectionDto", msgInspectionDto);
        return prefix + "/edit";
    }

    /**
     * 修改保存巡查台账
     */
    @RequiresPermissions("system:dto:edit")
    @Log(title = "巡查台账", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MsgInspectionDto msgInspectionDto)
    {
        return toAjax(msgInspectionDtoService.updateMsgInspectionDto(msgInspectionDto));
    }

    /**
     * 删除巡查台账
     */
    @RequiresPermissions("system:dto:remove")
    @Log(title = "巡查台账", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(msgInspectionDtoService.deleteMsgInspectionDtoByIds(ids));
    }
}
